﻿namespace OrderingFoot.Core
{
    /// <summary>
    /// 当前请求信息
    /// </summary>
    public interface ICurrentInfo
    {
        /// <summary>
        /// 微信小程序openid
        /// </summary>
        string MiniProgramOpenId { get; }

        /// <summary>
        /// 用户id
        /// </summary>
        Guid UserId { get; }

        /// <summary>
        /// 用户名称
        /// </summary>
        string UserName { get; }

        /// <summary>
        /// 是否管理平台
        /// </summary>
        bool IsPlatform { get; }
    }

    /// <summary>
    /// 当前请求信息
    /// </summary>
    public class CurrentInfo : ICurrentInfo
    {
        private readonly IHttpContextAccessor _contextAccessor;

        /// <summary>
        /// ctor
        /// </summary>
        public CurrentInfo(IHttpContextAccessor contextAccessor)
        {
            _contextAccessor = contextAccessor;
        }

        /// <summary>
        /// 微信小程序openid
        /// </summary>
        public string MiniProgramOpenId
        {
            get
            {
                return GetMiniProgramOpenId();
            }
        }

        /// <summary>
        /// 用户id
        /// </summary>
        public Guid UserId
        {
            get
            {
                var userid = IsPlatform ? GetPlatformUserId() : GetBaseUserID();
                try
                {
                    return Guid.Parse(userid);
                }
                catch
                {
                    return Guid.Empty;
                }
            }
        }

        /// <summary>
        /// 用户名称
        /// </summary>
        public string UserName => IsPlatform ? GetPlatformUserName() : GetBaseUserName();

        /// <summary>
        /// 是否管理平台
        /// </summary>
        public bool IsPlatform
        {
            get
            {
                if (_contextAccessor?.HttpContext != null)
                {
                    return _contextAccessor.HttpContext.Request.Headers.ContainsKey("p");
                }
                else
                {
                    return false;
                }
            }
        }

        #region private methods
        /// <summary>
        /// 用户微信小程序ID
        /// </summary>
        /// <returns></returns>
        private string GetMiniProgramOpenId()
        {
            return _contextAccessor?.HttpContext?.User?.Claims?.FirstOrDefault(m => m.Type == "sub")?.Value;
        }

        /// <summary>
        /// 用户ID
        /// </summary>
        /// <returns></returns>
        private string GetBaseUserID()
        {
            return _contextAccessor?.HttpContext?.User?.Claims?.FirstOrDefault(m => m.Type == "id")?.Value;
        }

        /// <summary>
        /// 用户名称
        /// </summary>
        /// <returns></returns>
        private string GetBaseUserName()
        {
            return _contextAccessor?.HttpContext?.User?.Claims?.FirstOrDefault(m => m.Type == "name")?.Value;
        }

        /// <summary>
        /// 获取平台用户ID
        /// </summary>
        private string GetPlatformUserId()
        {
            return _contextAccessor.HttpContext.Request.Headers["pu"];
        }

        /// <summary>
        /// 获取平台用户名称
        /// </summary>
        private string GetPlatformUserName()
        {
            return _contextAccessor.HttpContext.Request.Headers["pn"];
        }
        #endregion
    }
}
